<template>
	<div class="View">
		<div class="title">
			<i class="iconfont"></i>
			{{ $t("message.environmentSetting.list.title_10") }}
		</div>

		<div class="btn-wrapper" style="padding-top: 16px;">
			<el-select class="bgFFF" v-model="List1Value" placeholder=" " style="margin: 0 20px;" @change="changeFn">
				<el-option v-for="(item, index) in List1" :key="item.key" :label="item.label" :value="item.key">
				</el-option>
			</el-select>
			<div style="display: flex;">
				<el-button size="mini" style="width: 32px;max-width: 32px;" @click="sort('top')">
					<i class="el-icon-caret-top"></i>
				</el-button>
				<el-button size="mini" style="width: 32px;max-width: 32px;" @click="sort('bottom')">
					<i class="el-icon-caret-bottom"></i>
				</el-button>
				<el-button @click="addEdit">
					{{ $t("message.environmentSetting.btns.topBtn.save") }}
				</el-button>
				<el-button @click="deltDataFn">
					{{ $t("message.environmentSetting.btns.topBtn.del") }}
				</el-button>
			</div>
		</div>
		<div
			style="border-top:1px solid #e9e6e6;border-left:1px solid #e9e6e6;border-right:1px solid #e9e6e6;padding: 5px 10px;font-weight: 700;">
			図面関連付け
		</div>
		<ux-grid :data="tableData" :border="true" :header-cell-style="{
			color: 'rgba(0, 0, 0, 0.85)',
			fontSize: '14px',
			textAlign: 'left',
			fontWeight: 'bold',
			padding: 0,
			height: '32px',
			background: '#F5F5F5',
		}" :cell-style="{
			color: 'rgba(96,98,102,1)',
			fontSize: '14px',
			textAlign: 'left',
			padding: 0,
			height: '32px',
		}" :row-style="{
	color: 'rgba(96,98,102,1)',
	fontSize: '13px',
	textAlign: 'left',
	padding: 0,
	height: '32px',
}" max-height="758" ref="uxGrid" :height="tableHeight" v-loading="loading" element-loading-spinner="el-icon-loading"
			@cell-click="handClick" element-loading-background="#f8f8f8"
			:element-loading-text="$t('message.bhView.loading')">
			<template slot="empty"> No data </template>
			<!-- <ux-table-column style="pointer-events:none;" type="checkbox" width="35"> </ux-table-column> -->
			<ux-table-column property="codName" title="ブロック番号" width="255">
				<template slot-scope="scope">
					<el-input :value="`${leftLabel}${scope.row.index}`" class="table-input yellow" readonly>
					</el-input>
				</template>
			</ux-table-column>
			<ux-table-column property="codName" title="ブロック名" width="255">
				<template slot-scope="scope">
					<el-input v-model="scope.row.flowLabel" class="table-input"
						@input="scope.row.flowLabel = $setMaxLength(scope.row.flowLabel, 30)">
					</el-input>
				</template>
			</ux-table-column>
			<ux-table-column property="codName" title="圧力/温度(1)" width="255">
				<template slot-scope="scope">
					<el-input v-model="scope.row.prstmpLabel1" class="table-input"
						@input="scope.row.prstmpLabel1 = $setMaxLength(scope.row.prstmpLabel1, 30)">
					</el-input>
				</template>
			</ux-table-column>
			<ux-table-column property="codName" title="圧力/温度(2)">
				<template slot-scope="scope">
					<el-input v-model="scope.row.prstmpLabel3" class="table-input"
						@input="scope.row.prstmpLabel3 = $setMaxLength(scope.row.prstmpLabel3, 30)">
					</el-input>
				</template>
			</ux-table-column>
		</ux-grid>


		<div class="footer-wrapper">
			<el-button @click="getFluidPreTemperatureDataListFn">
				{{ $t("message.environmentSetting.btns.bottomBtn.editor") }}
			</el-button>
			<el-button type="primary" class="okBTn" @click="saveFn">
				{{ $t("message.environmentSetting.btns.bottomBtn.save") }}
			</el-button>
		</div>
	</div>
</template>

<script>
import {
	getFluidPreTemperatureDataList, getDetialsStyleGroup, getDelFluidPreTemperatureDataTipsMsg, delFluidPreTemperatureData, addFluidPreTemperatureData, getFluidPreTemperatureBlockLabel
} from "@/api/api";
import $ from "jquery";
export default {
	name: "Unit",
	data() {
		return {
			clsName: '',

			loading: false,
			tableHeight: 350,

			tableData: [],
			List1: [],
			List1Value: '',
			handRow: {},
			leftLabel: '',
		};
	},
	components: {},
	watch: {},
	methods: {
		swapArrayElements(array, index1, index2) {
			[array[index1], array[index2]] = [array[index2], array[index1]];
			// if (preservedKey !== undefined && preservedKey in array[index1] && preservedKey in array[index2]) {
			// 	[array[index1][preservedKey], array[index2][preservedKey]] = [array[index2][preservedKey], array[index1][preservedKey]];
			// }
			return array;
		},

		sort(sor) {
			const index = this.tableData.findIndex(item => item == this.handRow); // 获取到当前下表

			if (index == 0 && sor == 'top') {
				console.log('最上 return');
				return
			}
			if (index == this.tableData.length - 1 && sor == 'bottom') {
				console.log('最下 return');
				return
			}
			if (sor == 'top') {
				this.swapArrayElements(this.tableData, index, index - 1);
			}
			if (sor == 'bottom') {
				this.swapArrayElements(this.tableData, index, index + 1);
			}
			console.log(sor, index);
			this.$set(this.tableData, index, this.tableData[index]);
		},
		addEdit() {
			this.tableData.push({
				ctgCode: this.List1Value,
				flowLabel: '',
				compNo: 0,
				prstmpLabel1: "",
				prstmpLabel3: "",
				ordr: this.tableData.length == 0 ? 1 : this.tableData[this.tableData.length - 1].ordr + 1,
				index: this.tableData.length + 1
			})
		},
		handClick(e) {
			console.log(e);
			this.handRow = e;
		},
		async changeFn(e) {
			await this.getFluidPreTemperatureDataListFn();
		},
		// 获取大组下拉框
		async getList1Fn() {
			this.List1.length = 0;
			let D = {};
			let R = await getDetialsStyleGroup(D);
			this.List1 = R.data.data;
			try {
				this.List1Value = this.List1[0].key;
			} catch (error) {
			}
		},
		//获取详细仕样列表数据
		async getFluidPreTemperatureDataListFn() {
			this.loading = true;
			// 调用 wait 函数等待 3 秒后执行后续代码
			this.tableData.splice(0, this.tableData.length);
			let D = { CtgCode: this.List1Value };
			let R;
			R = await getFluidPreTemperatureDataList(D);
			this.loading = false;
			this.tableData = R.data.data;
			this.tableData.forEach((item, index) => {
				item.index = index + 1
			})
			this.handRow = this.tableData[0];
			this.$refs.uxGrid.setCurrentRow(this.tableData[0]);
		},
		//削除tab1
		async getDeltMsgFn() {
			let response;
			const Index = this.tableData.findIndex(item => item == this.handRow) + 1; // 获取到当前下表
			response = await getDelFluidPreTemperatureDataTipsMsg({ Index });
			return response.data.msg;
		},

		async deltDataFn() {
			const message = await this.getDeltMsgFn();
			try {
				await this.$confirm(message, '', {
					confirmButtonText: 'OK',
					cancelButtonText: 'Cancel',
					type: 'warning'
				});

				await delFluidPreTemperatureData({ CtgCode: this.handRow.ctgCode, CompNo: this.handRow.compNo });

				await this.getFluidPreTemperatureDataListFn();
			} catch (error) {
			}
		},
		// 保存
		async getFluidPreTemperatureBlockLabelFn() {
			let response;
			response = await getFluidPreTemperatureBlockLabel({});
			this.leftLabel = response.data.data;
		},
		async saveFn() {
			let D = { CtgCode: this.List1Value, DataList: this.tableData };
			let R = await addFluidPreTemperatureData(D);
			if (!R) {
				return
			}
			this.$message.success(R.data.msg);
			await this.getFluidPreTemperatureDataListFn();
		},
		async initFn() {

			await this.getFluidPreTemperatureBlockLabelFn();
			await this.getList1Fn();
			await this.getFluidPreTemperatureDataListFn();
		}
	},
	created() { },
	mounted() {

		this.initFn();

		let _this = this;
		this.tableHeight = $(".View").height() - 225;
		this.$nextTick(function () {
			window.addEventListener(
				"resize",
				() => {
					return (() => {
						_this.tableHeight = $(".View").height() - 225;
					})();
				},
				false
			);
		});
	},
	beforeDestroy() { },
	destroyed() { },
};
</script>
<style lang="scss" scoped>
.View {
	height: 100%;
	width: 100%;
	padding-left: 32px;
	position: relative;

	.title {
		width: 100%;
		height: 32px;
		border-bottom: 1px solid #d8d8d8;
		display: flex;
		align-items: center;

		.iconfont {
			margin-right: 10px;
		}

	}

	.tabs-view {
		width: 100%;
		padding: 16px 0;
	}

	.tab-title {
		width: 200px;
		height: 32px;
		background: #ffffff;
		border: 1px solid #dee0e3;
		text-align: center;
		line-height: 32px;
		font-size: 14px;
		color: #1f2329;
		border-right: 0 none;
		transition: 0.5s;
	}

	.active-style {
		background: #106ebe;
		color: #ffffff;
	}

	.tab-title:nth-child(1) {
		border-radius: 4px 0 0 4px;
	}

	.tab-title:nth-child(3) {
		border-right: 1px solid #dee0e3;
		border-radius: 0 4px 4px 0;
	}

	.btn-wrapper {
		display: flex;
		align-items: center;
		justify-content: space-between;
		margin-bottom: 20px;

		.bgFFF {
			::v-deep .el-input__inner {
				width: 240px;
				height: 32px;
				background: #FFF;
			}

			::v-deep .el-select__caret {
				line-height: 32px;
			}

		}


		button {
			display: flex;
			align-items: center;
			justify-content: center;
			width: 68px;
			height: 32px;
			// border: 1px solid #d9d9d9;
			margin-left: 16px;
		}

		button:nth-child(1) {
			width: 32px;
			font-size: 18px;
		}

		button:nth-child(2) {
			width: 32px;
			font-size: 18px;
		}

		.active-button {
			background: #106ebe;
			color: #ffffff;
		}
	}

	.table-input {
		::v-deep .el-input__inner {
			background: none;
			border: 0 none;
			padding: 0;
			padding-left: 8px;
		}
	}

	.yellow {
		::v-deep .el-input__inner {
			background: #ffffe1 !important;
		}
	}
}


::v-deep .c--ellipsis {
	padding: 0;
}

::v-deep .elx-header--column {
	padding-left: 8px !important;
}


.footer-wrapper {
	position: absolute;
	left: 32px;
	bottom: 50px;
	height: 32px;
	display: flex;

	button {
		width: 123px;
		height: 32px;
		// background: #ffffff;
		// border: 1px solid #d9d9d9;
		text-align: center;
		line-height: 32px;
		margin-right: 16px;
	}

	.okBTn {
		// background: #106ebe;
		// color: #fff;
	}
}
</style>
